import request from '@/utils/request.js'

/**
 * 聊天相关API
 */
export default {
	/**
	 * 创建或获取聊天会话
	 */
	createSession(targetUserId) {
		return request.post('/api/chat/session/create', {
			targetUserId: targetUserId
		})
	},

	/**
	 * 获取用户的会话列表
	 */
	getUserSessions() {
		return request.get('/api/chat/sessions')
	},

	/**
	 * 获取会话消息列表
	 */
	getSessionMessages(sessionId, page = 1, size = 20) {
		console.log('API调用 getSessionMessages:', { sessionId, page, size, sessionIdType: typeof sessionId })
		// 确保sessionId存在
		if (!sessionId) {
			console.error('sessionId为空，无法获取消息')
			return Promise.reject(new Error('sessionId为空'))
		}
		
		// 确保sessionId是数字或字符串
		const finalSessionId = String(sessionId)
		const url = `/api/chat/messages/${finalSessionId}?page=${page}&size=${size}`
		console.log('最终请求URL:', url)
		
		return request.get(url)
	},

	/**
	 * 标记消息为已读
	 */
	markMessagesAsRead(sessionId) {
		return request.post(`/api/chat/messages/read/${sessionId}`)
	},

	/**
	 * 获取未读消息数
	 */
	getUnreadCount(sessionId) {
		return request.get(`/api/chat/messages/unread/${sessionId}`)
	},

	/**
	 * 删除会话
	 */
	deleteSession(sessionId) {
		return request.delete(`/api/chat/session/${sessionId}`)
	}
} 